package com.ruoyi.project.zerocarbon.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.zerocarbon.domain.DeclareJudge;
import com.ruoyi.project.zerocarbon.domain.DeclareRanking;
import com.ruoyi.project.zerocarbon.mapper.DeclareJudgeMapper;
import com.ruoyi.project.zerocarbon.mapper.DeclareRankingMapper;
import com.ruoyi.project.zerocarbon.service.IDeclareJudgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 项目评审Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-31
 */
@Service
public class DeclareJudgeServiceImpl implements IDeclareJudgeService 
{
    @Autowired
    private DeclareJudgeMapper declareJudgeMapper;
    @Autowired
    private DeclareRankingMapper declareRankingMapper;

    /**
     * 查询项目评审
     * 
     * @param id 项目评审ID
     * @return 项目评审
     */
    @Override
    public DeclareJudge selectDeclareJudgeById(Long id)
    {
        return declareJudgeMapper.selectDeclareJudgeById(id);
    }

    /**
     * 查询项目评审列表
     * 
     * @param declareJudge 项目评审
     * @return 项目评审
     */
    @Override
    public List<DeclareJudge> selectDeclareJudgeList(DeclareJudge declareJudge)
    {
        return declareJudgeMapper.selectDeclareJudgeList(declareJudge);
    }

    /**
     * 新增项目评审
     * 
     * @param declareJudge 项目评审
     *                   A:90
     *
     *                   B:80
     *
     *                   C:70
     *
     *                   D:60
     *
     *                   E:50
     * @return 结果
     */
    @Override
    public int insertDeclareJudge(DeclareJudge declareJudge)
    {
        declareJudge.setCreateTime(DateUtils.getNowDate());
//        double v;
//        if (declareJudge.getManagementType() == 1){
//            int g1 = declareJudge.getGrade1();
//            int g2 = declareJudge.getGrade2();
//            int g3 = declareJudge.getGrade3();
//            int g4 = declareJudge.getGrade4();
//            v = 0.3 * g1 + (0.3 * g2) + (0.2 * g3) + (0.2 * g4);
//        }else {
//            int g1 = declareJudge.getGrade1();
//            int g2 = declareJudge.getGrade2();
//            int g3 = declareJudge.getGrade3();
//            v = 0.3 * g1 + (0.3 * g2) + (0.4 * g3);
//        }
//        declareJudge.setScore((int) Math.ceil(v));
        declareJudge.setStatus(1);
        int i = declareJudgeMapper.insertDeclareJudge(declareJudge);
        updateScore(declareJudge);
        return i;
    }

    /**
     * 修改项目评审
     * 
     * @param declareJudge 项目评审
     * @return 结果
     */
    @Override
    public int updateDeclareJudge(DeclareJudge declareJudge)
    {
//        double v;
//        if (declareJudge.getManagementType() == 1){
//            int g1 = declareJudge.getGrade1();
//            int g2 = declareJudge.getGrade2();
//            int g3 = declareJudge.getGrade3();
//            int g4 = declareJudge.getGrade4();
//            v = 0.3 * g1 + (0.3 * g2) + (0.2 * g3) + (0.2 * g4);
//        }else {
//            int g1 = declareJudge.getGrade1();
//            int g2 = declareJudge.getGrade2();
//            int g3 = declareJudge.getGrade3();
//            v = 0.3 * g1 + (0.3 * g2) + (0.4 * g3);
//        }
//        declareJudge.setScore((int) Math.ceil(v));
        declareJudge.setStatus(1);

        declareJudge.setUpdateTime(DateUtils.getNowDate());
        int i = declareJudgeMapper.updateDeclareJudge(declareJudge);
        updateScore(declareJudge);
        return i;
    }

    private void updateScore(DeclareJudge declareJudge){
        //更新平均分
        DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declareJudge.getDeclarationId());
        if (declareRanking == null){
            declareRanking = new DeclareRanking();
            declareRanking.setDeclarationId(declareJudge.getDeclarationId());
            DeclareJudge declareJudge2 = new DeclareJudge();
            declareJudge2.setDeclarationId(declareJudge.getDeclarationId());
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge2);
            declareRanking.setScore(calScore(declareJudges));
            declareRanking.setManagementType(declareJudge.getManagementType());
            declareRankingMapper.insertDeclareRanking(declareRanking);
        }else {
            DeclareJudge declareJudge2 = new DeclareJudge();
            declareJudge2.setDeclarationId(declareJudge.getDeclarationId());
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge2);
            declareRanking.setScore(calScore(declareJudges));
            declareRanking.setManagementType(declareJudge.getManagementType());
            declareRankingMapper.updateDeclareRanking(declareRanking);
        }
    }

    private int calScore(List<DeclareJudge> declareJudges){
        if (CollectionUtils.isEmpty(declareJudges)){
            return 0;
        }
//        if (declareJudges.size() < 4){
//            return 0;
//        }else if (declareJudges.size() == 4){
//            Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
//            return Math.round(sum/4);
//        }else if (declareJudges.size() == 5){
//            Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
//            return Math.round(sum/5);
//        }
        Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
        return Math.round(sum/4);
    }

    /**
     * 批量删除项目评审
     * 
     * @param ids 需要删除的项目评审ID
     * @return 结果
     */
    @Override
    public int deleteDeclareJudgeByIds(Long[] ids)
    {
        return declareJudgeMapper.deleteDeclareJudgeByIds(ids);
    }

    /**
     * 删除项目评审信息
     * 
     * @param id 项目评审ID
     * @return 结果
     */
    @Override
    public int deleteDeclareJudgeById(Long id)
    {
        return declareJudgeMapper.deleteDeclareJudgeById(id);
    }

}
